﻿using DAL;
using Microsoft.Extensions.Logging;
using Model;

namespace BLL
{
    public class WardBLL
    {
        /// <summary>
        /// 依赖注入BaseDAL
        /// </summary>
        BaseDAL<WardModel> dal;
        ILogger<WardBLL> logger;

        public WardBLL(BaseDAL<WardModel> dal, ILogger<WardBLL> logger)
        {
            this.dal = dal;
            this.logger = logger;
        }

        /// <summary>
        /// 添加科室
        /// </summary>
        /// <param name="w">科室实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddWard(WardModel w)
        {
            try
            {
                //1、科室名称不能重复
                var list=await dal.Getasync(x=>x.WardName==w.WardName);
                if (list!=null)
                {
                    //科室名称已存在
                    return -1;
                }
                //2、添加
                logger.LogInformation($"在{DateTime.Now}添加了一个{w.WardName}的科室");
                return await dal.Addasync(w);
            }
            catch (Exception ex)
            {
                logger.LogError("添加失败",ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 显示科室列表
        /// </summary>
        /// <param name="wardName">科室名称</param>
        /// <returns>返回受影响行数</returns>
        public IQueryable<WardModel> ShowWard(string? wardName="") 
        {
            try
            {
                //1、显示所有
                var list = dal.Query();
                //2、根据科室名称模糊查询
                if (!string.IsNullOrEmpty(wardName)) 
                {
                    list=list.Where(x=>x.WardName.Contains(wardName));
                }
                //3、返回集合
                return list;    
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
